package ee.ts022.tuum.services.ws.utils;

import java.sql.Date;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ee.ts022.tuum.services.ws.generated.ContractStatusType;
import ee.ts022.tuum.services.ws.generated.ContractType;
import ee.ts022.tuum.services.ws.generated.CstAddress;
import ee.ts022.tuum.services.ws.generated.Customer;
import ee.ts022.tuum.services.ws.generated.Employee;

public class DomainSoapParser {
	
	private static final Logger logger = LoggerFactory.getLogger(DomainSoapParser.class);



	public static Employee parseEmployee(ee.ts022.tuum.domain.Employee e) {
		if(e == null) {return null;}
		
		Employee result = new Employee();
		result.setEmployee(e.getEmployee());
		result.setFirstName(e.getFirstName());
		result.setLastName(e.getLastName());
		return result;
	}



	public static XMLGregorianCalendar parseDate(Date created) {
		if(created == null) {return null;}
		
		try {
			return DatatypeFactory.newInstance().newXMLGregorianCalendar(created.toString());
		} catch (DatatypeConfigurationException e) {
			logger.error("Could not parse date ["+created.toString()+"]");
		}
		
		return null;
	}

	public static ContractType parseContractType(
			ee.ts022.tuum.domain.ContractType contractType) {
		if(contractType == null) {return null;}
		
		ContractType result = new ContractType();
		result.setContractType(contractType.getContractType());
		result.setName(contractType.getName());
		return result;
	}

	public static ContractStatusType parseContractStatusType(
			ee.ts022.tuum.domain.ContractStatusType contractStatusType) {
		if(contractStatusType == null) {return null;}
		
		ContractStatusType result = new ContractStatusType();
		result.setContractStatusType(contractStatusType.getContractStatusType());
		result.setName(contractStatusType.getName());
		return result;
	}



	public static Customer parseCustomer(ee.ts022.tuum.domain.Customer customer) {
		if(customer == null) {return null;}
		
		Customer result = new Customer();
		result.setCustomer(customer.getCustomer());
		result.setFirstName(customer.getFirstName());
		result.setLastName(customer.getLastName());
		result.setIdentityCode(customer.getIdentityCode());
		result.setBirthDate(parseDate(customer.getBirthDate()));
		
		return result;
	}



	public static CstAddress parseCstAddress(
			ee.ts022.tuum.domain.CstAddress cstAddress) {
		if(cstAddress == null) {return null;}
		
		CstAddress result = new CstAddress();
		result.setCustomer(cstAddress.getCustomer().getCustomer());
		result.setAddress(cstAddress.getAddress());
		if(cstAddress.getAddressType() != null)
			result.setAddressType(cstAddress.getAddressType());
		result.setCountry(cstAddress.getCountry());
		result.setCstAddress(cstAddress.getCstAddress());
		result.setHouse(cstAddress.getHouse());
		result.setTownCounty(cstAddress.getTownCounty());
		result.setZip(cstAddress.getZip());
		
		return result;
	}

}
